﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
namespace Fulcrum.ViewModelGenerator
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics;

    #endregion Usings

    public class ViewModelAttachedPropertyXmlConfiguration : IViewModelAttachedPropertyConfiguration
    {
        #region Ctor

        public ViewModelAttachedPropertyXmlConfiguration(XElement xml)
        {
            if (null == xml)
            {
                throw new ArgumentNullException("xml");
            }

            if ("AttachedProperty" != xml.Name)
            {
                throw new ArgumentException("Element 'AttachedProperty' expected", "xml");
            }

            this.Parse(xml);
        }

        #endregion Ctor

        #region Parse

        private void Parse(XElement xml)
        {
            Debug.Assert(null != xml && "AttachedProperty" == xml.Name);

            this.Name = this.ParseName(xml);
            this.Type = (string)xml.RequiredAttribute("Type");
            this.DefaultValue = (string)xml.RequiredAttribute("DefaultValue");

            this.ValidateValue = (bool)xml.OptionalAttribute("ValidateValue", false);
            this.IsReadOnly = (bool)xml.OptionalAttribute("IsReadonly", false);

            this.GetMethodSummary = (string)xml.RequiredAttribute("GetSummary");
            this.SetMethodSummary = (string)xml.RequiredAttribute("SetSummary");
        }

        private string ParseName(XElement xml)
        {
            var name = (string)xml.RequiredAttribute("Name");

            Debug.Assert(name.Length > 0);
            if (Char.IsLower(name[0]))
            {
                throw new ArgumentException("The property name must start with a uppercase letter", "Name");
            }

            return name;
        }

        #endregion Parse

        #region Properties

        public string DefaultValue
        {
            get;
            private set;
        }

        public bool IsReadOnly
        {
            get;
            private set;
        }

        public string Name
        {
            get;
            private set;
        }

        public string Type
        {
            get;
            private set;
        }

        public bool ValidateValue
        {
            get;
            private set;
        }

        public string GetMethodSummary
        {
            get;
            private set;
        }

        public string SetMethodSummary
        {
            get;
            private set;
        }

        public string DependencyPropertyName
        {
            get
            {
                return String.Format("{0}Property", this.Name);
            }
        }

        public string DependencyPropertyKeyName
        {
            get
            {
                return String.Format("{0}PropertyKey", this.Name);
            }
        }

        public string StaticChangedMethodName
        {
            get
            {
                return String.Format("{0}_PropertyChanged", this.DependencyPropertyName);
            }
        }

        public string StaticValidateMethodName
        {
            get
            {
                return String.Format("{0}_ValidateProperty", this.DependencyPropertyName);
            }
        }

        public string StaticPartialValidateMethodName
        {
            get
            {
                return String.Format("{0}_ValidatePropertyImplementation", this.DependencyPropertyName);
            }
        }

        #endregion Properties
    }
}
